package com.example.common.tools

import android.media.MediaPlayer
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.lang.Exception
import java.util.concurrent.TimeUnit

class SoundUtils {

    private var mediaPlayer: MediaPlayer? = null

    private var listener: CompletionListener? = null
    private var plListener: PreparedListener? = null
    var disposable: Disposable? = null

    companion object {
        val instance by lazy(LazyThreadSafetyMode.NONE) {
            SoundUtils()
        }

        const val TAG = "mediaPlayer"
    }

    private var time = 0

    fun playSound(path: String): SoundUtils {
        var dataStr = path.split("/")
        var fileTruePath = "/sdcard"
        for (i in 4 until dataStr.size) {
            fileTruePath = fileTruePath + "/" + dataStr[i]
        }
        LogUtils.eLog(TAG, "播放文件路径 =  $fileTruePath")
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer()
        }
        mediaPlayer!!.setOnCompletionListener(MediaPlayer.OnCompletionListener { mp ->
            mp.stop()
            mp.reset()
            listener?.onCompletion()
        })
        mediaPlayer!!.setOnPreparedListener {
            plListener?.onPrepared(it, this)
        }
        try {
            if (!mediaPlayer!!.isPlaying) {
                mediaPlayer!!.setDataSource(fileTruePath)
                mediaPlayer!!.prepare()
                mediaPlayer!!.start()
            }
        } catch (e: Exception) {
            LogUtils.eLog("mediaPlayer", "音频播放失败 = ${e.message}")
            "音频播放失败".toast()
        }
        return this
    }

    fun stop() {
        if (mediaPlayer!!.isPlaying) {
            mediaPlayer!!.stop()
            mediaPlayer!!.reset()
        }
        if (disposable!=null && !disposable!!.isDisposed){
            disposable!!.dispose()
        }
    }

    fun playTimer(duration: Long, interval: Int, leng: Int): Observable<Int> {
        var size = 0
        return TimerUtils.instance.timer(
            duration,
            TimeUnit.MILLISECONDS
        ).map {
            if (it.toInt() % interval == 0 && size < leng) {
                return@map size++
            } else {
                return@map -1
            }
        }.observeOn(AndroidSchedulers.mainThread())
    }

    fun pause() {
        if (mediaPlayer!!.isPlaying) {
            mediaPlayer!!.pause()
        }
    }

    fun destroy() {
        if (mediaPlayer != null) {
            if (mediaPlayer!!.isPlaying) {
                mediaPlayer!!.stop()
                mediaPlayer!!.reset()
            }
            mediaPlayer = null
            disposable = null
        }
    }

    fun setCompletionListener(listener: CompletionListener): SoundUtils {
        this.listener = listener
        return this
    }

    fun setOnPreparedListener(listener: PreparedListener): SoundUtils {
        this.plListener = listener
        return this
    }

    interface CompletionListener {
        fun onCompletion()
    }

    interface PreparedListener {
        fun onPrepared(mp: MediaPlayer, soundUtil: SoundUtils)

    }

}

